<template>
  <div class="info">
    <template>
      <el-skeleton :loading="loading" />
    </template>
    <template v-if="!loading">
      <el-form :model="searchInfo" inline>
        <el-form-item>
          <el-input clearable v-model="searchInfo.name" placeholder="请输入食物名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
          <el-button icon="el-icon-refresh-right" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form inline v-if="showRoute">
        <el-form-item>
          <el-button type="success" @click="openWin()">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="infoList" border>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-form class="table-form" inline label-position="left" label-width="100px">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="编号:">
                    <span>{{scope.row.item_id}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="食物名:">
                    <span>{{scope.row.name}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="参加活动:">
                    <span>{{scope.row.activity}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="特色:">
                    <el-tag class="tag" type="success" v-for="(item,index) in scope.row.attributes" :key="index">{{item.icon_name}}</el-tag>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="商品描述:">
                    <span>{{scope.row.description}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="所在店铺:">
                    <span>{{scope.row.shopName}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="商品规格:">
                    <div v-for="(item,index) in scope.row.specfoods" :key="index">
                      <el-tag class="tag">规格名：{{item.name}}</el-tag>
                      <el-tag class="tag">打包费：{{item.packing_fee}}</el-tag>
                      <el-tag class="tag">商品价格：{{item.price}}</el-tag>
                    </div>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="食物图片:">
                    <el-image style="height: 100px;width: 100px" fit="fill" :src="'http://192.168.26.105:8082/file/getImgStream?fileName='+scope.row.image_path"></el-image>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="食物名" prop="name"/>
        <el-table-column label="食物描述" prop="description"/>
        <el-table-column label="所在店铺" prop="shopName" show-overflow-tooltip/>
        <el-table-column label="销售情况" prop="tips"/>
        <el-table-column label="是否启用" prop="state">
          <template slot-scope="scope">
            <el-button type="success" v-if="scope.row.state === '1'" size="small" @click="changeState(scope.row)" >启用</el-button>
            <el-button type="danger" v-else size="small" @click="changeState(scope.row)">禁用</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="openWin(scope.row)">修改</el-button>
            <el-button type="danger" size="small" @click="delInfo(scope.row.item_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="total,prev,pager,next,sizes,jumper"
                     :total="pageInfo.total"
                     :page-size="pageInfo.size"
                     :current-page="pageInfo.current"
                     :page-sizes="[5,10,20,30,50,100]"
                     @size-change="sizeChange"
                     @current-change="currentChange"
      />
    </template>
  </div>
</template>

<script>
export default {
  name: 'Food',
  data () {
    return {
      loading: true,
      searchInfo: {
        restaurant_id: '',
        name: '',
        page: 1,
        limit: 5
      },
      infoList: [],
      pageInfo: {},
      showRoute: true
    }
  },
  methods: {
    getList () {
      this.$http.get('shopping/v2/foods', this.searchInfo).then(({ data }) => {
        this.loading = false
        if (data.code === 20000) {
          this.infoList = data.data.records
          this.pageInfo = data.data
        }
      })
    },
    sizeChange (limit) {
      this.searchInfo.limit = limit
      this.getList()
    },
    currentChange (page) {
      this.searchInfo.page = page
      this.getList()
    },
    openWin (info) {
      console.log(info)
      if (info) {
        this.$router.push({ name: 'foodEdit', params: info })
      } else {
        this.$router.push('food')
      }
    },
    delInfo (id) {
      this.$confirm('确定删除该数据吗？', '询问').then(() => {
        this.$axios.delete('shopping/v2/food/' + id).then(({ data }) => {
          if (data.code === 20000) {
            this.$message({
              message: '删除成功',
              type: 'success',
              onClose: () => {
                this.getList()
              }
            })
          } else {
            this.$alert('删除失败！', { type: 'error' })
          }
        })
      })
    },
    resetForm () {
      this.searchInfo.name = ''
      this.getList()
    },
    init (shopId) {
      this.showRoute = false
      if (shopId) {
        this.searchInfo.restaurant_id = shopId
        this.getList()
      } else {
        this.$message.error('没有指定店铺ID，展示所有商品')
      }
    },
    changeState (food) {
      this.$confirm(`确定${food.state === '1' ? '禁用' : '启用'}该食物吗？`).then(() => {
        this.$http.post('/shopping/auditFood', { item_id: food.item_id, state: food.state === '1' ? -1 : 1 })
          .then(({ data }) => {
            if (data.code === 20000) {
              this.$message.success('更改状态成功')
              this.getList()
            }
          })
      })
    }
  },
  created () {
    this.$nextTick(() => {
      if (!this.searchInfo.restaurant_id) {
        this.getList()
      }
    })
  }
}
</script>

<style scoped lang="scss">
.tag{
  margin: 0 5px;
}
</style>
